import { config } from 'config.js'
const tips = {
  1: '抱歉，您的网络不稳定!',
  40001: '没有更多的订单了!'
}
// # 解构
class HTTP {
  request({ url, data = {}, method = 'POST' }) {
    return new Promise((resolve, reject) => {
      this._request(url, resolve, reject, data, method)
    })
  }
  _request(url, resolve, reject, data = {}, method = 'POST') {
    wx.request({
      url: config.api_blink_url + url,
      method: method,
      data: data,
      header: {
        'content-type': 'application/json',
      },
      success: (res) => {
        console.log(res)
        const code = res.statusCode //小程序返回的成功码
        if (code == 200) {
          resolve(res.data)
        }
        else {
          reject()
          const error_code = res.data.error_code
          this._show_error(error_code)
        }
      },
      fail: (err) => {
        reject()
        this._show_error(1)
      }
    })

  }

  _show_error(error_code) {
    if (!error_code) {
      error_code = 1
    }
    const tip = tips[error_code]
    wx.showToast({
      title: tip ? tip : tips[1],
      icon: 'none',
      duration: 1500
    })
  }


}

export { HTTP }


















